package kr.co.boogui.dao;

import java.util.List;

import mybatis3.helper.MVCSqlSessionFactory;

import org.apache.ibatis.session.SqlSession;

import kr.co.boogui.model.NewsVO;
import kr.co.boogui.model.ParamVO;

public class NewsDAO {

	private static int perpage = 9;

	public static List<NewsVO> All_listNews(int startno, String ftype,
			String fkey,int newstype) {
		List<NewsVO> result = null;
		SqlSession sess = MVCSqlSessionFactory.openSession();
		ParamVO pvo = new ParamVO();
		pvo.setStartno(startno);
		pvo.setFtype(ftype);
		pvo.setFkey(fkey);
		pvo.setPerpage(perpage);
		pvo.setNewstype(newstype);
		
		try {
			result = sess.selectList("NewsMapper.choiceNewsSQL", pvo);
		} finally {
			sess.close();
		}
		return result;
	}
	
	public static List<NewsVO> manyNews(int newstype){
		List<NewsVO> result =null;
		SqlSession sess = MVCSqlSessionFactory.openSession();
		ParamVO pvo = new ParamVO();
		pvo.setNewstype(newstype);
		
	try{
		result=sess.selectList("NewsMapper.manyNewsSQL",pvo);
	}finally{
		sess.close();
	}
	return result;
	}
		
	public static int totalNews(String ftype, String fkey, int newstype) {

		int result = 0;
		SqlSession sess = MVCSqlSessionFactory.openSession();
		ParamVO pvo = new ParamVO();
		pvo.setFkey(fkey);
		pvo.setFtype(ftype);
		pvo.setNewstype(newstype);
		try {
			result = sess.selectOne("NewsMapper.totalSQL", pvo);
		} finally {
			sess.close();
		}
		return result;
	}

	public static boolean writeNews(NewsVO n) {

		boolean result = false;
		SqlSession sess = null;
		try {
			sess = MVCSqlSessionFactory.openSession();
			if (sess.insert("BoardMapper.writeSQL", n) > 0) {
				result = true;
			}
			sess.commit(); // insert나 update 시 반드시 작성해야함
		} finally {
			sess.close();
		}

		return result;
	}

	public static NewsVO viewBoard(String newsno) {
		NewsVO result = null;
		SqlSession sess = null;
		try {
			sess = MVCSqlSessionFactory.openSession();
			System.out.println("newsno는?" + newsno);
			result = sess.selectOne("NewsMapper.viewnewsSQL", newsno);
		} finally {
			sess.close();
		}
		return result;
	}

	public static int viewCountNews(String newsno) {
		int result = 0;
		SqlSession sess = null;
		try {
			sess = MVCSqlSessionFactory.openSession();
			result = sess.update("NewsMapper.viewcountSQL", newsno);
			sess.commit();
		} finally {
			sess.close();
		}
		return result;
	}
}
